﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;
using Commonlib;
using System.Collections;

namespace AlbonWeb.Order.ashx
{
    /// <summary>
    /// Summary description for styleSelect
    /// </summary>
    public class styleSelect : IHttpHandler
    {
        private readonly BLL.StyleBLL stylebll = new BLL.StyleBLL();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write(getStyleList(context));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        protected string getStyleList(HttpContext context)
        {
            StringBuilder ret = new StringBuilder();
            if (context.Request["pid"] != null)
            {
                
                string pid = context.Request["pid"].ToString();
                string selectmode = "select selectMode from Style where ID=" + pid;
                string mode = SqlHelper.ExecuteScalar(SqlHelper.SqlConnectionString, CommandType.Text, selectmode, null).ToString();
                string cmdstr = "select * from Style where active=1 and parentid=" + pid;
                DataTable dt = SqlHelper.ExecuteDataSet(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null).Tables[0];
                
                switch (mode)
                {
                    case "Single":
                        foreach (DataRow dr in dt.Rows)
                        {
                            ret.Append(@"<div class='style_item' id='"+dr["ID"].ToString()+"' onclick='selectitem(" + pid + "," + dr["ID"].ToString() + ",this.innerHTML,this)' ");
                            if (Boolean.Parse(dr["buyClothMark"].ToString()))
                            {
                                ret.Append("style='color:red'");
                            }
                            ret.Append(">" + dr["styleName"].ToString() + "</div>");
                        }
                        break;
                    case "Multi":
                        string idlist = "0";
                        foreach (DataRow dr in dt.Rows)
                        {
                            ret.Append(@"<div class='style_item' id='" + dr["ID"].ToString()+"' ><input type='checkbox' id='option_"+dr["ID"].ToString() );
                            ret.Append(@"' onclick='selectOption(this,this.checked," + pid + "," + dr["ID"].ToString() + ")'");
                            if (context.Request["valuelist"] != null)
                            {
                                string[] values = context.Request["valuelist"].ToString().Split(',');
                                foreach (string value in values)
                                {
                                    if (value == dr["styleName"].ToString())
                                    {
                                        idlist += ","+dr["ID"].ToString();
                                        ret.Append(@" checked='checked'");
                                        break;
                                    }
                                }
                                
                            }
                            ret.Append(" /><label for='" + dr["ID"].ToString() + "' ");
                            if (Boolean.Parse(dr["buyClothMark"].ToString()))
                            {
                                ret.Append(@" style='color:red'");
                            }
                            ret.Append(@">" + dr["styleName"] + "</label></div>");
                            
                        }
                        Hashtable ht=stylebll.getRule(pid, idlist);
                        if (ht != null)
                        {
                            ArrayList rule = (ArrayList)ht["0"];
                            for (int i = 0; i < rule.Count; i++)
                            {
                                ret.Replace("id='option_" + rule[i] + "'", "id='option_" + rule[i] + "' disabled='disabled'");
                            }
                        }
                        break;
                    default:
                        string title = "You can also input a value on the right!";
                        ret.Append(@"<div class='style_item unExist' onclick='selectitem(" + pid + ",-1,this.innerHTML,this)' title='" + title + "' >" + "No Value" + "</div>");
                        if (getNameLabel(context).Trim() != "")
                        {
                            ret.Append(@"<div class='style_item' onclick='selectitem(" + pid + ",-1,this.innerHTML,this)' title='" + title + "' ");
                            ret.Append(">" + getNameLabel(context) + "</div>");
                        }
                        ret.Append(@"<div style='color:red'>" + title + "</div>");
                        break;
                }
                
            }
            //返回和当前keyvalue相关的规则
            else if (context.Request["keyid"] != null)
            {
                Hashtable ht = stylebll.getRule(context.Request["keyid"].ToString(), context.Request["selectedlist"].ToString());
                string rule = "";
                if (ht != null)
                {
                    if (ht.Contains("0"))
                    {

                        ArrayList list = (ArrayList)ht["0"];
                        rule = "0-";
                        for (int i = 0; i < list.Count; i++)
                        {
                            rule += list[i] + ",";
                        }
                        ret.Append(rule.Substring(0, rule.Length - 1));
                    }
                    else
                    {
                        ArrayList list = (ArrayList)ht["1"];
                        rule = "1-";
                        for (int i = 0; i < list.Count; i++)
                        {
                            rule += list[i] + ",";
                        }
                        ret.Append(rule.Substring(0, rule.Length - 1));
                    }

                }
            }
            return ret.ToString();
        }

        //获取一个订单的客户的袖名
        protected string getNameLabel(HttpContext context)
        {
            if (context.Request["orderid"] != null)
            {
                BLL.OrderBLL orderbll = new BLL.OrderBLL();
                DataTable dt = orderbll.getOrderByID(context.Request["orderid"].ToString());
                string clientName = dt.Rows[0]["clientName"].ToString();
                BLL.ClientBLL clientbll = new BLL.ClientBLL();
                return  clientbll.getNameLabel(clientName);
            }
            else return "";
        }
    }
}